package com.example.seh.util;


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.seh.R;
import com.example.seh.constant.Constant;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RegistActivity extends AppCompatActivity {
    private TextView getCode;
    private EditText phone, name, password, passwordConfirm, code;
    private Button registBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        getViews();
        setListeners();
    }

    private void setListeners() {
        getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PhoneUtil.isMobileNO(phone.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "验证码已发送", Toast.LENGTH_SHORT).show();
                    getCode();
                } else {
                    Toast.makeText(getApplicationContext(), "请正确输入手机号", Toast.LENGTH_SHORT).show();
                }

            }
        });
        registBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                msgVerify();
            }
        });
    }

    private void msgVerify() {
        if (msgIsEmpty()) {
            FormBody body = new FormBody.Builder()
                    .add("phone", phone.getText().toString())
                    .add("username", name.getText().toString())
                    .add("password", password.getText().toString())
                    .add("code", code.getText().toString())
                    .build();
            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request.Builder()
                    .post(body)
                    .url(Constant.REGIST)
                    .build();
            Log.e("地址：", request.url().toString());
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.e("okhttp", "获取数据失败");
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String result = response.body().string();
                    Log.e("regist", result);
                    if ("注册成功".equals(result)) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                        finish();
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        } else {
            Toast.makeText(getApplicationContext(), "请检查信息是否完整或正确", Toast.LENGTH_SHORT).show();
        }

    }

    private boolean msgIsEmpty() {
        if (!"".equals(phone.getText().toString())
                && !"".equals(name.getText().toString())
                && !"".equals(password.getText().toString())
                && !"".equals(passwordConfirm.getText().toString())
                && !"".equals(code.getText().toString())) {
            if (password.getText().toString().equals(passwordConfirm.getText().toString())) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    private void getCode() {
        Log.e("getcode",phone.getText().toString());
        FormBody body = new FormBody.Builder()
                .add("phone",phone.getText().toString())
                .build();
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .post(body)
                .url(Constant.GETCODE)
                .build();
        Log.e("地址：",request.url().toString());
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("okhttp", "获取数据失败");
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String isLogin = jsonObject.getString("data");
                    Log.e("LoginData",isLogin);
//                    JSONObject object = new JSONObject(isLogin);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void getViews() {
        phone = findViewById(R.id.regist_phone);
        name = findViewById(R.id.regist_name);
        password = findViewById(R.id.regist_password);
        passwordConfirm = findViewById(R.id.regist_confirm_password);
        code = findViewById(R.id.regist_captcha);
        getCode = findViewById(R.id.get_captcha);
        registBtn = findViewById(R.id.btn_regist);
    }

    private void sendSMS(String phoneNumber, String message) {
        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNumber, null, message, null, null);
            Toast.makeText(getApplicationContext(), "短信发送成功！", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            System.out.println("---------------------------" + e.getMessage());
            Toast.makeText(getApplicationContext(), "短信发送失败：" + e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
}